package com.erpanderp.wam.ac.view.reimbursement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import com.erpanderp.wam.ac.model.entity.reimbursement.BoardingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.Currency;
import com.erpanderp.wam.ac.model.entity.reimbursement.LodgingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TransportExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelMiscExpense;
import com.erpanderp.wam.ac.model.entity.travel.TravelExpenseAmount;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.ac.model.service.reimbursement.TravelReimbursementDAO;
import com.erpanderp.wam.ac.model.service.travel.TravelRequestDAO;
import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;

@ViewScoped
@ManagedBean
public class ViewOrPrintTravelReimbursementBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private TimeZone timeZone = TimeZone.getDefault();
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

	private String employeeCode;
	private String travelCode;
	private String travelExpenseCode;
	private String activity;
	private float approvalTotal;
	
	private EmployeeDAO employeeDAO = new EmployeeDAO();
	private TravelReimbursementDAO travelReimbursementDAO = new TravelReimbursementDAO();
	private TravelRequestDAO travelRequestDAO = new TravelRequestDAO();
	private CustomLookupDAO customLookupDAO = new CustomLookupDAO();

	private Employee employee = new Employee();
	private TravelExpense travelExpense=new TravelExpense();
	private TravelRequest travelRequest = new TravelRequest();
	
	private List<TransportExpense> transportExpensesDetailsList = new ArrayList<TransportExpense>(); 
	private List<LodgingExpense> lodgingExpenseDetailsList = new ArrayList<LodgingExpense>();
	private List<BoardingExpense> boardingExpenseDetailsList = new ArrayList<BoardingExpense>();
	private List<TravelMiscExpense> travelMiscExpenseDetailsList = new ArrayList<TravelMiscExpense>();
	private List<Currency> Currencies  =  new ArrayList<Currency>();
	private List<Currency> transportCurrencies = new ArrayList<Currency>();
	private List<Currency> lodgingCurrencies = new ArrayList<Currency>();
	private List<Currency> boardingCurrencies = new ArrayList<Currency>();
	private List<Currency> miscCurrencies = new ArrayList<Currency>();
	private List<LookupValue> lookUpValuesList = new ArrayList<LookupValue>();
	
	private Map<String, String> currencyMap = new HashMap<String, String>();
	private List<TravelExpenseAmount> policyAmounts = new ArrayList<TravelExpenseAmount>();
	private List<TravelExpenseAmount> advanceTakenAmounts = new ArrayList<TravelExpenseAmount>();
	private List<TravelExpenseAmount> approvalAmounts = new ArrayList<TravelExpenseAmount>();
	private List<TravelExpenseAmount> balanceAmounts = new ArrayList<TravelExpenseAmount>();
	
	private Employee employeeVP;

	public ViewOrPrintTravelReimbursementBean() {

		employeeCode=request.getParameter("employeeCode");
		travelCode=request.getParameter("travelCode");
		travelExpenseCode=request.getParameter("expenseCode");
		activity=request.getParameter("activity");
		  
		employee=employeeDAO.getEmployeeByEmployeeCode(employeeCode);
		travelRequest = travelRequestDAO.getTravelRequest(travelCode);
		employeeVP = employeeDAO.getVP();
		
		travelExpense= travelReimbursementDAO.getTravelExpenseByTravelExpenseCode(travelExpenseCode);
		
		transportExpensesDetailsList.addAll(travelExpense.getTransportExpenses());
		lodgingExpenseDetailsList.addAll(travelExpense.getLodgingExpenses());
		boardingExpenseDetailsList.addAll(travelExpense.getBoardingExpenses());
		System.out.println("SIZE: " + travelExpense.getTravelMiscExpenses().size());
		travelMiscExpenseDetailsList.addAll(travelExpense.getTravelMiscExpenses());
	
		if(travelExpense.getApprovalComments() == null || travelExpense.getApprovalComments().equals("")){
			travelExpense.setApprovalComments("Pending...");
		}if(travelExpense.getRecommenderComments() == null || travelExpense.getRecommenderComments().equals("")){
			travelExpense.setRecommenderComments("Pending...");
		}
		
		lookUpValuesList = customLookupDAO.getLookupValuesListByCustomLookupName(CommonLookup.CURRENCY_CUSTOM_LOOKUP);
		for (LookupValue lookUpValue : lookUpValuesList) {
			currencyMap.put(lookUpValue.getLookupName(),
					lookUpValue.getLookupValue());
		}
		
		for(TravelExpenseAmount expenseAmount : travelRequest.getTravelExpenseAmounts()){
			if(expenseAmount.getAmountType().equals(CommonLookup.TRAVEL_EXPENSE_TYPE_ADVANCE))
				advanceTakenAmounts.add(expenseAmount);
			if(expenseAmount.getAmountType().equals(CommonLookup.TRAVEL_EXPENSE_TYPE_APPROVAL_AMOUNT))
				approvalAmounts.add(expenseAmount);
			if(expenseAmount.getAmountType().equals(CommonLookup.TRAVEL_EXPENSE_POLICY_AMOUNT))
				policyAmounts.add(expenseAmount);
			if(expenseAmount.getAmountType().equals(CommonLookup.TRAVEL_EXPENSE_TYPE_PAYABLE_RECIEVABLE))
				balanceAmounts.add(expenseAmount);
		}
		System.out.println("lookUpValuesList========CONSTR===="+lookUpValuesList.size());
		totalAmountandCurrencyList();		
	}
	
	//Total Amount LIST & Currency
		public void totalAmountandCurrencyList(){
			for(LookupValue currency : lookUpValuesList){
				for(TransportExpense transportExpense : transportExpensesDetailsList){	
					if(transportExpense.getTransportCurrency().equals(currency.getLookupValue())){
						//MATCHED = true;
						boolean ADDED = false;						
						
						for(Currency cur : Currencies){
							System.out.println(cur.getAmount() + cur.getCurrency() + " TTT");	
							
								
							if(currency.getLookupValue().equals(cur.getCurrency())){
								System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
								ADDED = true;
								//change the value
								cur.setAmount(cur.getAmount()+ transportExpense.getTransportAmount());								
								break;
							}
						}						
						if(!ADDED ){
							//create new currentcy and add to the list
							Currency currencyObj = new Currency();
							currencyObj.setAmount(transportExpense.getTransportAmount());
							currencyObj.setCurrency(transportExpense.getTransportCurrency());
							Currencies.add(currencyObj);
						}
						
						ADDED = false;
						for(Currency cur : transportCurrencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								//change the value
								cur.setAmount(cur.getAmount()+ transportExpense.getTransportAmount());								
								break;
							}
						}						
						if(!ADDED ){
							//create new currentcy and add to the list
							Currency currencyObj = new Currency();
							currencyObj.setAmount(transportExpense.getTransportAmount());
							currencyObj.setCurrency(transportExpense.getTransportCurrency());
							transportCurrencies.add(currencyObj);
						}
						//break;				
					}
				}
				for(LodgingExpense lodgingExpense : lodgingExpenseDetailsList){
					if(lodgingExpense.getLodgingCurrency().equals(currency.getLookupValue())){
						boolean ADDED = false;						
						for(Currency cur : Currencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								cur.setAmount(cur.getAmount()+ lodgingExpense.getLodgingAmount());								
								break;
							}
						}						
						if(!ADDED ){
							Currency currencyObj = new Currency();
							currencyObj.setAmount(lodgingExpense.getLodgingAmount());
							currencyObj.setCurrency(lodgingExpense.getLodgingCurrency());
							Currencies.add(currencyObj);
						}
						ADDED = false;
						for(Currency cur : lodgingCurrencies){
							System.out.println(cur.getAmount() + cur.getCurrency());
							if(currency.getLookupValue().equals(cur.getCurrency())){
								System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
								ADDED = true;
								//change the value
								cur.setAmount(cur.getAmount()+ lodgingExpense.getLodgingAmount());								
								break;
							}
						}						
						if(!ADDED ){
							//create new currentcy and add to the list
							System.out.println("NOT ADDED.");
							Currency currencyObj = new Currency();
							currencyObj.setAmount(lodgingExpense.getLodgingAmount());
							currencyObj.setCurrency(lodgingExpense.getLodgingCurrency());
							lodgingCurrencies.add(currencyObj);
						}
					}
				}
				//FOR BORDING
				for(BoardingExpense boardingExpense : boardingExpenseDetailsList){
					if(boardingExpense.getBoardingCurrency().equals(currency.getLookupValue())){
						boolean ADDED = false;						
						for(Currency cur : Currencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								cur.setAmount(cur.getAmount()+ boardingExpense.getBoardingAmount());								
								break;
							}
						}						
						if(!ADDED ){
							Currency currencyObj = new Currency();
							currencyObj.setAmount(boardingExpense.getBoardingAmount());
							currencyObj.setCurrency(boardingExpense.getBoardingCurrency());
							Currencies.add(currencyObj);
						}
						
						ADDED = false;
						for(Currency cur : boardingCurrencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								//change the value
								cur.setAmount(cur.getAmount()+ boardingExpense.getBoardingAmount());								
								break;
							}
						}						
						if(!ADDED ){
							//create new currentcy and add to the list
							Currency currencyObj = new Currency();
							currencyObj.setAmount(boardingExpense.getBoardingAmount());
							currencyObj.setCurrency(boardingExpense.getBoardingCurrency());
							boardingCurrencies.add(currencyObj);
						}
					}
				}
				//FOR Travell Misc expense
				for(TravelMiscExpense travelMiscExpense : travelMiscExpenseDetailsList){
					if(travelMiscExpense.getTravelMiscExpensecurrency().equals(currency.getLookupValue())){
						boolean ADDED = false;						
						for(Currency cur : Currencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								cur.setAmount(cur.getAmount()+ travelMiscExpense.getTravelMiscExpenseAmount());								
								break;
							}
						}						
						if(!ADDED ){
							Currency currencyObj = new Currency();
							currencyObj.setAmount(travelMiscExpense.getTravelMiscExpenseAmount());
							currencyObj.setCurrency(travelMiscExpense.getTravelMiscExpensecurrency());
							Currencies.add(currencyObj);
						}
						
						ADDED = false;
						for(Currency cur : miscCurrencies){
							if(currency.getLookupValue().equals(cur.getCurrency())){
								ADDED = true;
								//change the value
								cur.setAmount(cur.getAmount()+ travelMiscExpense.getTravelMiscExpenseAmount());								
								break;
							}
						}						
						if(!ADDED ){
							//create new currentcy and add to the list
							Currency currencyObj = new Currency();
							currencyObj.setAmount(travelMiscExpense.getTravelMiscExpenseAmount());
							currencyObj.setCurrency(travelMiscExpense.getTravelMiscExpensecurrency());
							miscCurrencies.add(currencyObj);
						}
					}
				}
				
			}
		}
		
		//SETTERS AND GETTERS
		public String getEmployeeCode() {
			return employeeCode;
		}
		public void setEmployeeCode(String employeeCode) {
			this.employeeCode = employeeCode;
		}

		public String getTravelCode() {
			return travelCode;
		}
		public void setTravelCode(String travelCode) {
			this.travelCode = travelCode;
		}
		
		public EmployeeDAO getEmployeeDAO() {
			return employeeDAO;
		}
		public void setEmployeeDAO(EmployeeDAO employeeDAO) {
			this.employeeDAO = employeeDAO;
		}
		
		public Employee getEmployee() {
			return employee;
		}
		public void setEmployee(Employee employee) {
			this.employee = employee;
		}

		public TravelReimbursementDAO getTravelReimbursementDAO() {
			return travelReimbursementDAO;
		}
		public void setTravelReimbursementDAO(TravelReimbursementDAO travelReimbursementDAO) {
			this.travelReimbursementDAO = travelReimbursementDAO;
		}

		public TravelExpense getTravelExpense() {
			return travelExpense;
		}
		public void setTravelExpense(TravelExpense travelExpense) {
			this.travelExpense = travelExpense;
		}

		public String getTravelExpenseCode() {
			return travelExpenseCode;
		}
		public void setTravelExpenseCode(String travelExpenseCode) {
			this.travelExpenseCode = travelExpenseCode;
		}

		public TravelRequest getTravelRequest() {
			return travelRequest;
		}
		public void setTravelRequest(TravelRequest travelRequest) {
			this.travelRequest = travelRequest;
		}
		
		public TravelRequestDAO getTravelRequestDAO() {
			return travelRequestDAO;
		}
		public void setTravelRequestDAO(TravelRequestDAO travelRequestDAO) {
			this.travelRequestDAO = travelRequestDAO;
		}

		public List<TransportExpense> getTransportExpensesDetailsList() {
			return transportExpensesDetailsList;
		}
		public void setTransportExpensesDetailsList(
				List<TransportExpense> transportExpensesDetailsList) {
			this.transportExpensesDetailsList = transportExpensesDetailsList;
		}

		public List<LodgingExpense> getLodgingExpenseDetailsList() {
			return lodgingExpenseDetailsList;
		}
		public void setLodgingExpenseDetailsList(
				List<LodgingExpense> lodgingExpenseDetailsList) {
			this.lodgingExpenseDetailsList = lodgingExpenseDetailsList;
		}

		public List<BoardingExpense> getBoardingExpenseDetailsList() {
			return boardingExpenseDetailsList;
		}
		public void setBoardingExpenseDetailsList(
				List<BoardingExpense> boardingExpenseDetailsList) {
			this.boardingExpenseDetailsList = boardingExpenseDetailsList;
		}

		public List<TravelMiscExpense> getTravelMiscExpenseDetailsList() {
			return travelMiscExpenseDetailsList;
		}
		public void setTravelMiscExpenseDetailsList(
			List<TravelMiscExpense> travelMiscExpenseDetailsList) {
			this.travelMiscExpenseDetailsList = travelMiscExpenseDetailsList;
		}

		public List<Currency> getCurrencies() {
			return Currencies;
		}
		public void setCurrencies(List<Currency> currencies) {
			Currencies = currencies;
		}

		public String getActivity() {
			return activity;
		}

		public void setActivity(String activity) {
			this.activity = activity;
		}
		public CustomLookupDAO getCustomLookupDAO() {
			return customLookupDAO;
		}

		public void setCustomLookupDAO(CustomLookupDAO customLookupDAO) {
			this.customLookupDAO = customLookupDAO;
		}

		public List<LookupValue> getLookUpValuesList() {
			return lookUpValuesList;
		}

		public void setLookUpValuesList(List<LookupValue> lookUpValuesList) {
			this.lookUpValuesList = lookUpValuesList;
		}
		public float getApprovalTotal() {
			return approvalTotal;
		}

		public void setApprovalTotal(float approvalTotal) {
			this.approvalTotal = approvalTotal;
		}

		public Employee getEmployeeVP() {
			return employeeVP;
		}

		public void setEmployeeVP(Employee employeeVP) {
			this.employeeVP = employeeVP;
		}

		public TimeZone getTimeZone() {
			return timeZone;
		}

		public void setTimeZone(TimeZone timeZone) {
			this.timeZone = timeZone;
		}

		public List<Currency> getTransportCurrencies() {
			return transportCurrencies;
		}

		public void setTransportCurrencies(List<Currency> transportCurrencies) {
			this.transportCurrencies = transportCurrencies;
		}

		public List<Currency> getLodgingCurrencies() {
			return lodgingCurrencies;
		}

		public void setLodgingCurrencies(List<Currency> lodgingCurrencies) {
			this.lodgingCurrencies = lodgingCurrencies;
		}

		public List<Currency> getBoardingCurrencies() {
			return boardingCurrencies;
		}

		public void setBoardingCurrencies(List<Currency> boardingCurrencies) {
			this.boardingCurrencies = boardingCurrencies;
		}

		public List<Currency> getMiscCurrencies() {
			return miscCurrencies;
		}

		public void setMiscCurrencies(List<Currency> miscCurrencies) {
			this.miscCurrencies = miscCurrencies;
		}

		public Map<String, String> getCurrencyMap() {
			return currencyMap;
		}

		public void setCurrencyMap(Map<String, String> currencyMap) {
			this.currencyMap = currencyMap;
		}

		public List<TravelExpenseAmount> getPolicyAmounts() {
			return policyAmounts;
		}

		public void setPolicyAmounts(List<TravelExpenseAmount> policyAmounts) {
			this.policyAmounts = policyAmounts;
		}

		public List<TravelExpenseAmount> getAdvanceTakenAmounts() {
			return advanceTakenAmounts;
		}

		public void setAdvanceTakenAmounts(List<TravelExpenseAmount> advanceTakenAmounts) {
			this.advanceTakenAmounts = advanceTakenAmounts;
		}

		public List<TravelExpenseAmount> getApprovalAmounts() {
			return approvalAmounts;
		}

		public void setApprovalAmounts(List<TravelExpenseAmount> approvalAmounts) {
			this.approvalAmounts = approvalAmounts;
		}

		public List<TravelExpenseAmount> getBalanceAmounts() {
			return balanceAmounts;
		}

		public void setBalanceAmounts(List<TravelExpenseAmount> balanceAmounts) {
			this.balanceAmounts = balanceAmounts;
		}


}
